package com.tnxst.phexamination.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;


import com.tnxst.phexamination.R;
import com.tnxst.phexamination.dto.ServicePackageDTO;

import java.util.List;

public class AddSignPackageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;

    private List<ServicePackageDTO> signPackages;

    private Onclick onclick;

    private boolean[] status;


    public AddSignPackageAdapter(List<ServicePackageDTO> signPackages) {
        this.signPackages = signPackages;
        status = new boolean[this.signPackages.size()];
    }

    public void setStatusTrue(List<ServicePackageDTO> save){
        if (signPackages == null || signPackages.size() == 0)
            return;
        status = new boolean[signPackages.size()];
        for (ServicePackageDTO servicePackageDTO : save) {
            for (int i = 0; i < signPackages.size(); i++) {
                ServicePackageDTO dto = signPackages.get(i);
                if (dto.getSERVICEID().equals(servicePackageDTO.getSERVICEID())){
                    status[i] = true;
                    break;
                }
            }
        }
    }

    public void setSignPackages(List<ServicePackageDTO> signPackages){
        this.signPackages = signPackages;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = View.inflate(parent.getContext(), R.layout.add_sign_package_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holders = (ViewHolder) holder;
        holders.aSwitch.setText(signPackages.get(position).getSERVICENAME());
        if (status[position]){
            holders.aSwitch.setChecked(true);
        }else {
            holders.aSwitch.setChecked(false);
        }
        holders.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed())
                    return;
                if (isChecked) {
                    onclick.add(signPackages.get(position));
                    status[position] = true;
                } else {
                    onclick.remove(signPackages.get(position));
                    status[position] = false;
                }
            }
        });
//        holders.aSwitch.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
//                View view = View.inflate(context, R.layout.package_info, null);
//                alertDialog
//                        .setView(view)
//                        .create();
//                TextView title = view.findViewById(R.id.text99);
//                TextView package_info = view.findViewById(R.id.text100);
//                title.setText(signPackages.get(position).getPackageName());
//                package_info.setText(signPackages.get(position).getPackageDescription());
//                final AlertDialog show = alertDialog.show();
//                return true;
//            }
//        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View view;

        Switch aSwitch;

        public ViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            aSwitch = itemView.findViewById(R.id.switch4);
        }
    }

    @Override
    public int getItemCount() {
        return signPackages.size();
    }


    public interface Onclick {

        void add(ServicePackageDTO signPackage);

        void remove(ServicePackageDTO signPackage);
    }

    ;

    public void setOnclick(Onclick onclick) {
        this.onclick = onclick;
    }
}
